<?php
namespace Index\Controller;
use Think\Controller;
class IndexController extends Controller {
    public function index(){


        // 循环分页
        $link =M();
		// $uid =$_SESSION['id'];
        $sql_all ="select count(*) cn from question";
        $res =current($link->query($sql_all))['cn'];
        // var_dump($res);
        $Page = new \Think\Page($res,6);
        // var_dump($page);
        $show =$Page->show();
        // var_dump($show);


    	$sql ="select t1.*,t2.username,t2.head_img,t3.cname from question t1 left join user t2 on t1.uid=t2.id left join category t3 on t3.id=t1.qid order by t1.time desc limit {$Page->firstRow},{$Page->listRows}";
    	// var_dump($sql);
    	$sql_cate ="select * from category where fid='0'";
    	$data = $link->query($sql_cate);
        // var_dump($data);





    	
    	// var_dump($data);
        $question = $link->query($sql);
        // var_dump($question);
        $this->assign('data',$data);//循环分类
        $this->assign('show',$show);//将分页循环到模板
        $this->assign('question',$question);//循环所有问题
        $this->display();
    }

 

 //*******************************查看问题
    public function question(){

         if ($_GET) {
            // var_dump($_GET);
            $id=$_GET['id'];
            // var_dump($id);
            $link =M();
            // $uid = $_SESSION['id'];
            $sql ="select q.*,u.username,u.head_img from question q left join user u on q.uid=u.id where q.id ={$id}";
            $data =current($link->query($sql));
            // var_dump($data);
            $data['content'] =html_entity_decode($data['content']);//将实体转为字符
            // var_dump($data);

             //浏览一次 view自增1次
            $view ="update question set view=view+1 where id={$id}";
            $result = $link->execute($view);

            //回复后将数据输出到模板
            $sql_an = "select a.*,u.username,u.head_img from answer a left join user u on a.uid =u.id where qid={$id}";
            $res = $link->query($sql_an);
            
            // var_dump($res);
            $this->assign('res',$res);
            $this->assign('data',$data);
            $this->display();

        }
    }

//*******************************8查看分类问题
    public function explore(){
        //循环分类
        $link =M();
        
        // var_dump($sql);
        $sql_cate ="select * from category where fid='0'";
        $data = $link->query($sql_cate);
        $this->assign('data',$data);//循环分类

        if ($_GET) {
            // var_dump($_GET);
            $sql_que ="select q.*,u.head_img,u.username from question q left join user u on u.id=q.uid where qid='{$_GET['id']}'";
            // var_dump($sql_que);
            $res =$link->query($sql_que);
            // var_dump($res);
            $this->assign('res',$res);//循环当前分类所有问题
        }
        $this->display();
    }



//*************************编辑问题
    public function question_edit(){
        //循环分类
        $link =M();
        $sql_cate ="select * from category";
        $cate =$link->query($sql_cate);
        // var_dump($cate);

        //将原问题数据输出
        $id = $_GET['id'];//获取当前问题id
        $link =M();
        $sql ="select q.*,c.cname,u.id from question q left join category c on q.qid=c.id left join user u on q.uid=u.id where q.id={$id}";
        $data =$link->query($sql);
        $data[0]['content'] =html_entity_decode($data[0]['content']);//将实体转为字符
        // var_dump($data)    ;
        $this->assign('cate',$cate);//输出分类到模板
        $this->assign('data',current($data));//将获取到的问题数据输出到模板

        if ($_POST) {
            $arr=[];
            $arr['title']=I('post.title');
            $arr['qid']=I('post.category_id');
            $arr['content']=I('post.editorValue');
            $arr['time']=date('Y-m-d H:m:s');
            // var_dump($arr);exit;
            $sql_edit ="update question set title='{$arr["title"]}',qid='{$arr["qid"]}',content='{$arr["content"]}',time='{$arr["time"]}' where id={$id}";
            // var_dump($sql_edit);
            $res =$link->execute($sql_edit);
            $this->success('编辑成功!',U('index/index/question',array('id'=>$id)));
        }
        $this->display();
    }



 //**********************回复功能
    public function answer(){

        if ($_POST) {
           // var_dump($_POST);
            $qid = I('post.question_id');//当前问题id
            // var_dump($qid);
            $id =$_SESSION['id'];
            $content = I('post.answer');
            $time = date('Y-m-d H:m:s');

            $link = M();
            $sql = "insert into answer(content,time,uid,qid) values('{$content}','{$time}','{$id}','{$qid}')";
            $data = $link->execute($sql);

            //回复一次 replay回复自增1次
            $replay ="update question set replay=replay+1 where id={$qid}";
            $data = $link->execute($replay);


            // var_dump($data);
            $this->success('回复成功!',U('index/index/question',array('id'=>$qid)));
            //将回复数据输出到模板
            // $sql_data = "select * from answer a left join question q on "
        }
    }

//**************************收藏回复
    public function collection(){
        $link =M();
        if ($_GET) {
            $id =$_GET['id'];//要收藏的回复的id
            // var_dump($id);
            $usr_id =$_SESSION['id'];//当前登陆用户的id
            $sql_an ="select qid from answer where id='{$id}'";
            $qid =current($link->query($sql_an));//获取当前问题的id
            // var_dump($qid);exit;
            $sql_exe ="select * from answer_collection where uid='{$usr_id}'";
            $res_exe =$link->query($sql_exe);
            foreach ($res_exe as  $value) {
                if ($value['aid']==$id) {
                    $this->error('您已收藏过了!',U('index/index/question',array('id'=>$qid['qid'])));
                }
            }
            
            $sql ="insert into answer_collection (aid,uid) values('{$id}','{$usr_id}')";
            $res =$link->execute($sql);
            // var_dump($qid);exit;
            $this->success('收藏成功!',U('index/index/question',array('id'=>$qid['qid'])));
        }
    }

//*********************************搜索
    public function search(){
        $q = I('post.q','');
        if (empty($q)) {

            $this->error('请输入你想要搜索的词');
            
        }else{
            header("Content-type: text/html; charset=utf-8");

            // $preg = "/({$q})/";
            // $q_new = preg_replace($preg, replacement, subject)
            $data = Cut($q);
            // var_dump( $data,$q);exit;
            if (empty($data)) {
                 $data[]['word'] = $q;
            }
            $sql = "select * from question where ";

            $cn ="";
            
            // var_dump($data);exit;
            foreach ($data as  $value) {
                $sql = $sql.$cn." "."title like '%".$value['word']."%'";
                $cn = "or";
            }

            // var_dump($sql);exit;
            $res =M()->query($sql);

            $this->assign('res',$res);
            // var_dump($res);
            $this->display();
        }
    }















}